package com.tuju.currency.wxapi;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tuju.currency.Constants;
import com.tuju.currency.bean.resp.WXAccessToken;
import com.tuju.currency.bean.resp.WXUserInfo;
import com.tuju.currency.network.DefaultSubscriber;
import com.tuju.currency.network.HttpClient;
import com.tuju.currency.utils.dialog.MyDialog;

public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private IWXAPI api;
    private int count = 0;
    private MyDialog myDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myDialog = MyDialog.showDialog(this);
        // 注册API
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                Log.e("flag", "-----code:ok");
                if (resp instanceof SendAuth.Resp) {
                    SendAuth.Resp sendAuthResp = (SendAuth.Resp) resp;
                    String code = sendAuthResp.code;
                    getAccessToken(code);
                    // 发起登录请求
                    Log.e("flag", "-----code:" + sendAuthResp.code);
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                if (resp instanceof SendAuth.Resp) {
                }
                Log.e("flag", "-----授权取消:");
                Toast.makeText(this, "授权取消:", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                if (resp instanceof SendAuth.Resp) {
                }
                Log.e("flag", "-----授权失败:");
                Toast.makeText(this, "授权失败:", Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                break;
        }
    }

    // 根据授权code  获取AccessToken
    public void getAccessToken(String code) {
        myDialog.show();
        HttpClient.getInstance().getAccessToken(Constants.APP_ID, Constants.APP_SECRET, code)
                .subscribe(new DefaultSubscriber<WXAccessToken>() {
                    @Override
                    public void _onError(String errMsg) {
                        Toast.makeText(WXEntryActivity.this, errMsg, Toast.LENGTH_SHORT).show();
                        error();
                    }

                    @Override
                    public void _onNext(WXAccessToken entity) {
                        String result = new Gson().toJson(entity);
                        Log.i("yanze", result);
                        getUserInfo(entity.getAccess_token(), entity.getOpenid());
                    }
                });
    }

    //获取UserInfo
    public void getUserInfo(String token, final String openId) {
        HttpClient.getInstance().getWXUserInfo(token, openId)
                .subscribe(new DefaultSubscriber<WXUserInfo>() {
                    @Override
                    public void _onError(String errMsg) {
                        Toast.makeText(WXEntryActivity.this, errMsg, Toast.LENGTH_SHORT).show();
                        error();
                    }

                    @Override
                    public void _onNext(WXUserInfo entity) {
                        String result = new Gson().toJson(entity);
                        Log.i("yanze", result);
                        Toast.makeText(WXEntryActivity.this, result, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    public void error() {
        myDialog.dismiss();
        finish();
    }

}
